var t = getApp();

Page({
    data: {
        baseUrl: t.globalData.baseUrl,
        cartNum: 0,
        totalPrice: 0,
        cateId: "",
        storeGoods: [],
        cateList: [],
        cartIdsStr: "",
        cartIdsList: [],
        allStoreGoods: [],
        chosGoodlist: [],
        choprojectList: [],
        hasChooseList: []
    },
    onLoad: function(a) {
        var e = t.globalData.urlBase + t.globalData.urlCateList;
        this.getinitData(e);
    },
    getinitData: function(a) {
        var e = this;
        wx.request({
            url: a,
            data: {
                storeId: t.globalData.storeId,
                serverId: t.globalData.serverId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var a = JSON.parse(t.data), o = [];
                if (0 != a.code) wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 2e3
                }); else if (a.catesList.length > 0) {
                    for (var s = 0; s < a.catesList.length; s++) o = o.concat(a.catesList[s].goodsList);
                    e.setData({
                        cateList: a.catesList,
                        cateId: a.catesList[0].id ? a.catesList[0].id : "",
                        storeGoods: a.catesList[0].goodsList ? a.catesList[0].goodsList : "",
                        allStoreGoods: o,
                        hasChooseList: []
                    });
                }
            }
        });
    },
    addNum: function(a) {
        for (var e = this, o = a.currentTarget.dataset.goodid, s = a.currentTarget.dataset.goodprice, r = e.data.storeGoods, i = 0; i < r.length; i++) if (r[i].id == o) {
            r[i].num = (r[i].num ? r[i].num : 0) + 1;
            var d = e.data.totalPrice;
            d += s;
            var c = r[i].name, l = r[i].img, n = r[i].price;
        }
        for (var u = e.data.hasChooseList, g = !1, i = 0; i < u.length; i++) u[i].id == o && (g = !0, 
        u[i].num = u[i].num + 1);
        if (!g) {
            var h = {};
            h.id = o, h.num = 1, h.name = c, h.img = l, h.price = n, u.push(h);
        }
        var L = e.data.cartNum + 1;
        t.globalData.serDetail.chosGoodlist = u, t.globalData.serDetail.totalPrice = d, 
        e.setData({
            totalPrice: d,
            cartNum: L,
            storeGoods: r,
            hasChooseList: u
        });
    },
    mulNum: function(a) {
        for (var e = this, o = a.currentTarget.dataset.goodid, s = a.currentTarget.dataset.goodprice, r = a.currentTarget.dataset.num, i = e.data.storeGoods, d = 0; d < i.length; d++) if (i[d].id == o) {
            i[d].num = (i[d].num > 1 ? i[d].num : 1) - 1;
            var c = e.data.totalPrice;
            if (r < 1) {
                c = c;
                l = e.data.cartNum;
            } else {
                c -= s;
                var l = e.data.cartNum - 1;
            }
        }
        var n = e.data.hasChooseList;
        if (1 == r) for (d = 0; d < n.length; d++) n[d].id == o && n.pop(d); else for (d = 0; d < n.length; d++) n[d].id == o && (n[d].num = n[d].num - 1);
        t.globalData.serDetail.chosGoodlist = n, t.globalData.serDetail.totalPrice = c, 
        e.setData({
            totalPrice: c,
            cartNum: l,
            storeGoods: i,
            hasChooseList: n
        });
    },
    chooseCate: function(t) {
        for (var a = this, e = [], o = {}, s = 0; s < a.data.cateList.length; s++) a.data.cateList[s].id == t.currentTarget.dataset.code && (o = a.data.cateList[s], 
        e = a.data.cateList[s].goodsList);
        for (var r = a.data.cartIdsList, s = e.length - 1; s >= 0; s--) {
            e[s].selected = 0;
            for (d = r.length - 1; d >= 0; d--) if (e[s].id == r[d]) {
                e[s].selected = 1;
                break;
            }
        }
        for (var i = a.data.hasChooseList, s = 0; s < e.length; s++) for (var d = 0; d < i.length; d++) if (e[s].id == i[d].id) {
            e[s].num = i[d].num;
            break;
        }
        a.setData({
            cateId: o.id,
            storeGoods: e
        });
    },
    sub: function() {
        var a = this;
        t.globalData.cartList = a.data.hasChooseList, t.globalData.cartList.length <= 0 ? wx.showToast({
            title: "请选择项目",
            icon: "none",
            duration: 2e3
        }) : wx.navigateTo({
            url: "../cTime/cTime"
        });
    },
    onShareAppMessage: function() {}
});